package com.cloud.adrian.novel.author.controller;

import com.cloud.adrian.novel.author.manager.feign.BookFeignManager;
import com.cloud.adrian.novel.author.novel.dto.req.AuthorRegisterReqDto;
import com.cloud.adrian.novel.author.service.AuthorService;
import com.cloud.adrian.novel.book.dto.req.BookAddReqDto;
import com.cloud.adrian.novel.book.dto.req.BookPageReqDto;
import com.cloud.adrian.novel.book.dto.req.ChapterAddReqDto;
import com.cloud.adrian.novel.book.dto.resp.BookChapterContentRespDto;
import com.cloud.adrian.novel.book.dto.resp.BookChapterRespDto;
import com.cloud.adrian.novel.book.dto.resp.BookInfoRespDto;
import com.cloud.adrian.novel.common.auth.UserHolder;
import com.cloud.adrian.novel.common.constant.ApiRouterConstants;
import com.cloud.adrian.novel.common.constant.SystemConfigConstants;
import com.cloud.adrian.novel.common.req.PageReqDto;
import com.cloud.adrian.novel.common.resp.PageRespDto;
import com.cloud.adrian.novel.common.resp.RestResp;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: xiangguiliang
 * @version: 1.0
 * @since: 2024/05/29
 */
@Tag(name = "AuthorController", description = "作家后台-管理模块")
@SecurityRequirement(name = SystemConfigConstants.HTTP_AUTH_HEADER_NAME)
@RestController
@RequestMapping(ApiRouterConstants.API_AUTHOR_URL_PREFIX)
@RequiredArgsConstructor
public class AuthorController {
    private final AuthorService authorService;

    private final BookFeignManager bookFeignManager;

    @Operation(summary = "查询作家状态 接口")
    @GetMapping("status")
    public RestResp<Integer> getStatus() {
        return authorService.getStatus(UserHolder.getUserId());
    }

    @Operation(summary = "作家注册接口")
    @PostMapping("register")
    public RestResp<Void> register(@Valid @RequestBody AuthorRegisterReqDto dto) {
        dto.setUserId(UserHolder.getUserId());
        return authorService.register(dto);
    }

    @Operation(summary = "小说发布接口")
    @PostMapping("book")
    public RestResp<Void> publishBook(@Valid @RequestBody BookAddReqDto dto) {
        return bookFeignManager.publishBook(dto);
    }

    @Operation(description = "小说发布列表查询接口")
    @GetMapping("books")
    public RestResp<PageRespDto<BookInfoRespDto>> listBooks(@ParameterObject BookPageReqDto dto) {
        return bookFeignManager.listPublishBooks(dto);
    }

    @Operation(description = "小说章节发布接口")
    @PostMapping("book/chapter/{bookId}")
    public RestResp<Void> publishBookChapter(
            @Parameter(description = "小说id") @PathVariable("bookId") Long bookId,
            @Valid @RequestBody ChapterAddReqDto dto) {
        return bookFeignManager.publishBookChapters(bookId, dto);
    }

    @Operation(description = "小说章节列表查询接口")
    @GetMapping("book/chapters/{bookId}")
    public RestResp<PageRespDto<BookChapterRespDto>> listBookChapters(
            @Parameter(description = "小说id") @PathVariable("bookId") Long bookId,
            @ParameterObject PageReqDto dto) {
        return bookFeignManager.listBookChapters(bookId, dto);
    }

    @Operation(description = "小说章节内容查询接口")
    @GetMapping("book/chapter/{id}")
    public RestResp<BookChapterContentRespDto> getChapter(
            @Parameter(description = "章节id") @PathVariable("id") Long id) {
        return bookFeignManager.getChapterContent(id);
    }

    @Operation(description = "小说章节修改接口")
    @PutMapping("book/chapter/{id}")
    public RestResp<Void> updateChapter(@Parameter(description = "章节id") @PathVariable("id") Long id,
                                        @Valid @RequestBody ChapterAddReqDto dto) {
        return bookFeignManager.updateChapter(id, dto);
    }

    @Operation(description = "小说章节删除接口")
    @DeleteMapping("book/chapter/{id}")
    public RestResp<Void> deleteChapter(@Parameter(description = "章节id") @PathVariable("id") Long id) {
        return bookFeignManager.deleteChapter(id);
    }

}
